package ldh.im.fxgui.pane;

import javafx.scene.Node;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import ldh.im.dto.enums.MessageType;
import ldh.im.fxbase.data.pojo.ImMessage;
import ldh.im.fxbase.richtext.ImText;
import ldh.im.fxbase.util.StageUtil;
import ldh.im.fxbase.util.ParamUtil;
import ldh.im.fxbase.util.StringUtil;

public class TextMessagePane extends BaseMessagePane {

    @Override
    public Node createPane(ImMessage message, boolean isReceive, boolean isRef) {
        if (message.getMessageType() != MessageType.Text) {
            throw new RuntimeException("不支持这种消息类型" + message.getMessageType());
        }

        TextAlignment textAlignment = TextAlignment.LEFT; //isReceive ? TextAlignment.LEFT : TextAlignment.RIGHT;
        ImText imText = new ImText(textAlignment);

        if (isRef) {
            String content = message.getContent();
            content = content.replace("\r", " ");
            content = content.replace("\n", " ");

            boolean isLongStr = StringUtil.stringBytesLength(content) > ParamUtil.MESSAGE_REF_LENGTH;
            String messageContent = StringUtil.subString(content, ParamUtil.MESSAGE_REF_LENGTH);
            if (isLongStr) {
                messageContent += "...";
            }

            Text text = new Text(messageContent);
            text.setTextAlignment(textAlignment);
            return text;
        } else {
            imText.setImContent(message.getContent());
            if (StageUtil.calcWidth(message.getContent()) >= ParamUtil.MESSAGE_WIDTH) {
                imText.getText().setWrappingWidth(ParamUtil.MESSAGE_WIDTH);
            }
        }

        buildPopup(imText, message);
        return imText;
    }
}
